window.onload =function (){
    showRoomData(1)
}

function showRoomData(pageNO){
    axios.get("/room/findByItem",{
        params:{
            pageNO,
            address:$("queryAddress").value,
            state:$("QRoomState").value,
            info:$("isRoomLive").value,
            type:$("roomType2").value
        }
    }).then(resp =>{
        var info = resp.data;

        if (info === "noland"){
            location.href="/login.html?errorInfo=noland";
        }
        var str="";
        info.list.forEach(n =>{
            str+=`<tr><td class="alt">${n.address}</td>
                      <td class="alt">${n.capacity}</td>
                      <td class="alt">${n.num}</td>
                      <td class="alt">${n.status}</td>
                      <td class="alt">${n.type}</td>
                    <td class="alt"><input class="button" type="button" value="删除房间" onclick="delRoom(${n.id})">
                    <input class="button" type="button" value="查看房间" onclick="showFindByRoomId(${n.id})">
                    <input class="button" type="button" value="查看设施" onclick="findFacById(${n.id})"></td></tr>`
        })
        $("data").innerHTML=str;

        var cutStr="";
        for (var i=1;i<=info.totalPage;i++){
            cutStr+=`<a href="javascript:showRoomData(${i})">${i}</a>`;
        }
        $("cutDiv").innerHTML=cutStr;

    })
}


function showQueryRoom(){
    $("queryRoomInfo").style.visibility="visible"
}

function queryRoom(){
    showRoomData(1)
    backToRoomMain()
}

function backToRoomMain(){
    $("queryRoomInfo").style.visibility="hidden";
    $("addNewRoom").style.visibility="hidden";
    $("findRoom").style.visibility="hidden";
    $("findFac").style.visibility="hidden";
}

function showAddRoom(){
    $("addNewRoom").style.visibility="visible"
}

function addNewRoom(){
    axios.get("/room/add",{
        params: {
            address:$("addRoomAddress").value,
            capacity:$("addRoomNum").value,
            type:$("roomType").value
            }
}).then(resp =>{
    var info = resp.data;
    if (info == "ok"){
        showRoomData(1)
        backToRoomMain()
    }
    })
}

function showFindByRoomId(id){
    $("findRoom").style.visibility="visible";
    axios.get("/room/findByRoomId",{
        params:{
            id
        }

    }).then(resp =>{
        var info=resp.data;
        $("findRoomAddress").innerHTML=info.address;
        $("findRoomStatus").innerHTML=info.status;
        $("findRoomType").innerHTML=info.type;
        $("findRoomCapacity").innerHTML=info.capacity;
        $("findRoomNum").innerHTML=info.num;
        var str="";
        var obj=info.stuList;
        console.log(obj)
        obj.forEach(n=>{
            str+=`<tr><td>${n.name}</td>
                      <td>${n.classBean.className}</td>
                      <td>${n.phone}</td>
                    </tr>`;
        })
        $("roomStu").innerHTML=str;
    })
}

function findFacById(id){
    $("findFac").style.visibility="visible";
    axios.get("/facilities/findByRoomId",{
        params:{
            roomId:id
        }
    }).then(resp =>{
        var info = resp.data;
        console.log(info)
        var str="";
        info.forEach(n=>{
            str+=`<tr><td class="alt">${n.describe}</td>
                      <td class="alt">${n.time}</td>
                      <td class="alt">${n.status}</td>
                      <td class="alt"><input class="button" type="button" value="处理设施" onclick="changeFac(${n.id},${n.roomBean.id})"></td>
                    </tr>`;
        })
        $("roomFac").innerHTML=str;
    })
    $("roomIdSpan").value=id;
}

function changeFac(fId,rId){
    axios.get("/facilities/update",{
        params:{
            facilityId:fId,
            roomId:rId
        }
    }).then(resp =>{
        var info = resp.data;
        if (info == "ok"){
            showRoomData(1);
            backToRoomMain();
            findFacById(rId);
        }
    })
}

function delRoom(id){
    axios.get("/room/del",{
        params:{
            roomId:id
        }
    }).then(resp =>{
        var info =resp.data;
        if (info === "ok"){
            showRoomData(1)
            backToRoomMain()
        }
    })
}

function addFac(){

    axios.get("/facilities/add",{
        params:{
            describe:$("FacDes").value,
            roomId:$("roomIdSpan").value
        }
    }).then(resp =>{
        var info = resp.data;
        if (info === "ok"){
            showRoomData(1);
            $("FacDes").value="";
            backToRoomMain();
        }
    })
}